﻿using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace DimensionsHelper.LanguageService.Syntax;

public abstract class PreExpression : SourceNode
{
    private SourceNode? _leftParenthesisToken;
    private SourceNode? _rightParenthesisToken;

    protected internal PreExpression(SyntaxKinds kind) : base(kind)
    {
    }


    protected internal PreExpression(SourceNode? parent, SyntaxKinds kind) :
        base(parent, kind)
    {
    }


    /// <summary>
    ///     表达式的左括号
    /// </summary>
    public SourceNode? LeftParenthesisToken => _leftParenthesisToken;


    /// <summary>
    ///     表达式的右括号
    /// </summary>
    public SourceNode? RightParenthesisToken => _rightParenthesisToken;


    /// <summary>
    ///     如果表达式是 defined() 表达式，返回<see langword="true" />
    /// </summary>
    public bool IsDefinedExpression => IsNode(SyntaxKinds.PreDefinedExpression);


    public override bool IsEmpty => false;


    public PreExpression Update(SourceNode? leftParenthesisToken, SourceNode? rightParenthesisToken)
    {
        Debug.Assert(leftParenthesisToken == null || leftParenthesisToken.IsNode(SyntaxKinds.LeftParenthesisToken));
        Debug.Assert(rightParenthesisToken == null ||
                     rightParenthesisToken.IsNode(SyntaxKinds.RightParenthesisToken));
        SetChildNullableField(ref _leftParenthesisToken, IsNullOrDefault, leftParenthesisToken);
        SetChildNullableField(ref _rightParenthesisToken, IsNullOrDefault, rightParenthesisToken);
        return this;
    }


    public static bool IsNullOrEmpty([NotNullWhen(false)] PreExpression? expression)
    {
        return expression == null || expression.IsEmpty;
    }
}